<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>料品主信息</title>
    <!-- Bootstrap CSS-->
    <link rel="stylesheet" href="../../static/css/lib/bootstrap3.4.1.min.css" th:href="@{/css/lib/bootstrap3.4.1.min.css}">
    <!-- Bootstrap-table CSS-->
    <link rel="stylesheet" href="../../static/css/lib/bootstrap-table.min.css" th:href="@{/css/lib/bootstrap-table.min.css}">
    <!-- jquery-ui CSS-->
    <link rel="stylesheet" href="../../static/css/lib/jquery-ui.min.css" th:href="@{/css/lib/jquery-ui.min.css}">
    <link href="../../static/css/file.css" rel="stylesheet" th:href="@{/css/file.css}">
    <link href="../../static/css/table.css" rel="stylesheet" th:href="@{/css/table.css}">
</head>

<body onload="init();">
    <div class="panel-body" style="padding-bottom:0;">
        <div class="panel panel-default">
            <div class="panel-heading">查询条件</div>
            <div class="panel-body">
                <div class="form-group" style="margin-top:15px">
                    <div class="row row-gap">
                        <label class="control-label col-sm-2 label-font" for="txt_search_item_name" title="品名／磁棒／尺寸(材质)">品名／磁棒／尺寸(材质)</label>
                        <div class="col-sm-4">
                            <input type="text" class="form-control" id="txt_search_item_name">
                        </div>
                        <label class="control-label col-sm-2 label-font" for="txt_search_spec" title="规格/初R值/电阻线(线径)">规格/初R值/电阻线(线径)</label>
                        <div class="col-sm-4">
                            <input type="text" class="form-control" id="txt_search_spec">
                        </div>
                    </div>
                    <div class="row row-gap">
                        <div class="col-sm-offset-4" style="text-align:left;">
                            <button type="button" style="margin-left:50px" id="btn_filter" class="btn btn-primary" onclick="filter();">过滤</button>
                            <button type="button" style="margin-left:50px" id="btn_reset_filter" class="btn btn-primary" onclick="init();">重置过滤</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div id="toolbar" class="btn-group">
            <button id="btn_add" type="button" class="btn btn-default" data-toggle="modal" data-target="#addModal">
                <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增料品主信息
            </button>
            <button id="btn_export" type="button" class="btn btn-default" onclick="exportExcel();">
                <span class="glyphicon glyphicon-export" aria-hidden="true"></span>批量导出
            </button>
        </div>
        <table id="table"></table>

    </div>

    <!-- 模态框（Modal） 查看 -->
    <div class="modal fade" id="viewModal" tabindex="-1" role="dialog" aria-labelledby="viewModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                        &times;
                    </button>
                    <h4 class="modal-title" id="viewModalLabel">
                        查看原材料主信息
                    </h4>
                </div>
                <div class="modal-body">
                    <table class="table">
                        <caption>原材料主信息</caption>
                        <tbody>
                        <tr>
                            <td>品名／磁棒／尺寸(材质)</td>
                            <td id="view_item_name">xxx</td>
                        </tr>
                        <tr>
                            <td>规格/初R值/电阻线(线径)</td>
                            <td id="view_spec">xxx</td>
                        </tr>
                        <tr>
                            <td>单位</td>
                            <td id="view_unit">xxx</td>
                        </tr>
                        <tr>
                            <td>备注</td>
                            <td id="view_remark">xxx</td>
                        </tr>
                        </tbody>
                    </table>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                    </button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>

    <!-- 模态框（Modal） 编辑 -->
    <div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                        &times;
                    </button>
                    <h4 class="modal-title" id="editModalLabel">
                        编辑原材料主信息
                    </h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form">
                        <div class="form-group has-success">
                            <label class="col-sm-4 control-label label-font" for="edit_item_name">品名／磁棒／尺寸(材质)</label>
                            <div class="col-sm-8">
                                <input class="form-control" type="text" id="edit_item_name" title="该输入项不能为空" onchange="checkInputChange('edit_item_name');">
                            </div>
                        </div>
                        <div class="form-group has-success">
                            <label class="col-sm-4 control-label label-font" for="edit_spec">规格/初R值/电阻线(线径)</label>
                            <div class="col-sm-8">
                                <input class="form-control" type="text" id="edit_spec" title="该输入项不能为空" onchange="checkInputChange('edit_spec');">
                            </div>
                        </div>
                        <div class="form-group has-success">
                            <label class="col-sm-4 control-label label-font" for="edit_unit">单位</label>
                            <div class="col-sm-8">
                                <input class="form-control" type="text" id="edit_unit" title="该输入项不能为空" onchange="checkInputChange('edit_unit');">
                            </div>
                        </div>
                        <div class="form-group has-success">
                            <label class="col-sm-4 control-label label-font" for="edit_remark">备注</label>
                            <div class="col-sm-8">
                                <textarea class="form-control" rows="6" id="edit_remark"></textarea>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-success" onclick="edit();">修改</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>

    <!-- 模态框（Modal） 新增 -->
    <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="addModalLabel" aria-hidden="true">
        <div class="modal-dialog" style="min-width: 800px">
            <div class="modal-content">
                <div class="modal-header">
                    <h2 class="modal-title text-center" id="addModalLabel">
                        添加原材料主信息信息
                    </h2>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form">
                        <div class="form-group has-error">
                            <label class="col-sm-4 control-label label-font" for="input_item_name">品名／磁棒／尺寸(材质)</label>
                            <div class="col-sm-8">
                                <input class="form-control" type="text" id="input_item_name" title="该输入项不能为空" onchange="checkInputChange('input_item_name');">
                            </div>
                        </div>
                        <div class="form-group has-error">
                            <label class="col-sm-4 control-label label-font" for="input_spec">规格/初R值/电阻线(线径)</label>
                            <div class="col-sm-8">
                                <input class="form-control" type="text" id="input_spec" title="该输入项不能为空" onchange="checkInputChange('input_spec');">
                            </div>
                        </div>
                        <div class="form-group has-error">
                            <label class="col-sm-4 control-label label-font" for="input_unit">单位</label>
                            <div class="col-sm-8">
                                <input class="form-control" type="text" id="input_unit" title="该输入项不能为空" onchange="checkInputChange('input_unit');">
                            </div>
                        </div>
                        <div class="form-group has-success">
                            <label class="col-sm-4 control-label label-font" for="input_remark">备注</label>
                            <div class="col-sm-8">
                                <textarea class="form-control" rows="6" id="input_remark"></textarea>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-success" onclick="add();">添加</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>

<!-- JavaScript files-->
<!-- jquery.js -->
<script src="../../static/js/lib/jquery.min.js" th:src="@{/js/lib/jquery.min.js}"></script>
<!-- Bootstrap.js -->
<script src="../../static/js/lib/bootstrap3.4.1.min.js" th:src="@{/js/lib/bootstrap3.4.1.min.js}"></script>
<!-- Bootstrap-table.js -->
<script src="../../static/js/lib/bootstrap-table.min.js" th:src="@{/js/lib/bootstrap-table.min.js}"></script>
<script src="../../static/js/lib/bootstrap-table-zh-CN.min.js" th:src="@{/js/lib/bootstrap-table-zh-CN.min.js}"></script>
<!-- sweetalert2.js -->
<script src="../../static/js/lib/sweetalert.min.js" th:src="@{/js/lib/sweetalert.min.js}"></script>
<script src="../../static/js/lib/es6-promise.auto.min.js" th:src="@{/js/lib/es6-promise.auto.min.js}"></script>
<!-- jquery-ui.js -->
<script src="../../static/js/lib/jquery-ui.min.js" th:src="@{/js/lib/jquery-ui.min.js}"></script>
<script src="../../static/js/common.js" th:src="@{/js/common.js}"></script>
<script src="../../static/js/dragModel.js" th:src="@{/js/dragModel.js}"></script>
<script>
    /** 初始化 */
    let $table = $('#table');

    // 服务器分页获取数据
    function getServerPaging(node, url, queryParams) {
        initTable(node, url, [10, 20, 30, 40, 50, 60, "All"], queryParams, [{
            checkbox: true
        }, {
            field: 'itemName',
            title: '品名／磁棒／尺寸(材质)',
            width: '170px',
            formatter: stringFormatter
        }, {
            field: 'spec',
            title: '规格/初R值/电阻线(线径)',
            width: '170px',
            formatter: stringFormatter
        },  {
            field: 'unit',
            title: '单位',
            width: '250px',
            formatter: stringFormatter
        }, {
            field: 'remark',
            title: '备注',
            width: '250px',
            formatter: stringFormatter
        },{
            field: 'createTime',
            title: '录入日期',
            width: '170px',
            formatter: dateFormatter
        }, {
            field: 'operation',
            title: '操作',
            width: '257px',
            formatter: operateFormatter // 自定义方法，添加操作按钮
        }]);
    }

    function operateFormatter(value, row, index) {  // 赋予的参数
        return [
            '<button class="btn btn-success view" data-toggle="modal" data-target="#viewModal" onclick="view('+ index +');">查看</button>&nbsp;&nbsp;' +
            '<button class="btn btn-primary view" data-toggle="modal" data-target="#editModal" onclick="initEdit('+ index +');">编辑</button>&nbsp;&nbsp;' +
            '<button class="btn btn-danger view" onclick="deleteOne('+ index +');">删除</button>&nbsp;&nbsp;'
        ].join('');
    }

    // 修改提示框样式
    changeToolTip();

    // 模态框初始化
    dragTheModalDialog();

    // 初始化表格数据
    function init() {
        getServerPaging($table, "/material-owner-information/getMaterialInfoMaster", params => {
            return { limit: params.limit, offset: params.offset, sort: "create_time", order: "DESC" };
        });
    }

</script>

<script>
    /** 功能 */
    // 过滤
    function filter() {
        let data = [$("#txt_search_item_name"), $("#txt_search_spec")];

        if ("" === data[0].val() && "" === data[1].val()) {
            swal("操作提示", "请填写过滤参数", "info");
            return;
        }

        getServerPaging($table, "/material-owner-information/getMaterialInfoMaster", params => {
            let result = { limit: params.limit, offset: params.offset, sort: "create_time", order: "DESC", itemName: data[0].val(), spec: data[1].val() };
            removeEmptyField(result);
            return result;
        });
    }

    // 导出excel
    function exportExcel() {
        let selections = $table.bootstrapTable('getSelections');
        if (selections.length === 0) {
            swal("操作提示", "请选择记录", "info");
        } else {
            parameterPostRequest("/material-owner-information/exportExcel", selections, () => loadFile("料品主信息.xlsx"));
        }
    }

    // 添加记录
    function add() {
        let data = {
            itemName: $("#input_item_name").val(),
            spec: $("#input_spec").val(),
            unit: $("#input_unit").val(),
            remark: $("#input_remark").val()
        };
        if ('' === data.itemName || '' === data.spec || '' === data.unit ) {
            swal("操作提示", "请输入必输入项", "info");
        } else {
            parameterPostRequest("/material-owner-information/insert", data, () => {
                $('#addModal').modal('hide');
                swal("正确", "添加成功", "success");
                init();
            });
        }
    }

    // 查看
    function view(index) {
        let rows = $table.bootstrapTable('getData');
        $("#view_item_name").text(rows[index].itemName);
        $("#view_spec").text(rows[index].spec);
        $("#view_unit").text(rows[index].unit);
        $("#view_remark").text(rows[index].remark);
    }

    // 编辑
    // edit记录id
    let editId;
    function initEdit(index) {
        let rows = $table.bootstrapTable('getData');
        $("#edit_item_name").val(rows[index].itemName).trigger("change");
        $("#edit_spec").val(rows[index].spec).trigger("change");
        $("#edit_unit").val(rows[index].unit).trigger("change");
        $("#edit_remark").val(rows[index].remark);
        editId = rows[index].id;
    }
    function edit() {
        let data = {
            id: editId,
            itemName: $("#edit_item_name").val(),
            spec: $("#edit_spec").val(),
            unit: $("#edit_unit").val(),
            remark:$("#edit_remark").val()
        };
        if (null === data.id || '' === data.itemName || '' === data.unit || '' === data.spec) {
            swal("操作提示", "请输入必输入项", "info");
        } else {
            parameterPostRequest("/material-owner-information/updateMaterialInfoMaster", data, () => {
                $('#editModal').modal('hide');
                swal("正确", "编辑成功", "success");
                init();
            });
        }
    }

    // 删除
    function deleteOne(index) {
        dialog("确定删除吗？", () => {
            let data = { "id": $table.bootstrapTable('getData')[index].id };
            parameterPostRequest("/material-owner-information/delete", data, () => {
                swal("正确", "删除成功", "success");
                init();
            });
        });
    }

</script>

</body>

</html>